package net.kts.attraction.subway.smallpass.process.thread;

import android.util.Log;
import net.kts.attraction.subway.smallpass.model.LastPath;
import net.kts.attraction.subway.smallpass.model.PassLine;
import net.kts.attraction.subway.smallpass.model.PassStation;
import net.kts.attraction.subway.smallpass.model.infomation.StationInfo;
import net.kts.attraction.subway.smallpass.process.SmallWayPass;
import net.kts.attraction.subway.smallpass.process.check.SmallWayChecker;

public class SmallWayDownDirectionThread implements Runnable {

	private LastPath lastPath = null;
	private StationInfo stationInfo = null;
	private SmallWayPass smallWayPass = null;
	private boolean flag = true;
	private boolean nextFlag = false;

	public SmallWayDownDirectionThread(SmallWayPass smallWayPass, LastPath lastPath, StationInfo stationInfo) {
		this.lastPath = lastPath;
		this.stationInfo = stationInfo;
		this.smallWayPass = smallWayPass;
	}



	@Override
	public void run() {
		// TODO Auto-generated method stub
		//		super.run();


		StationInfo current = this.stationInfo;
		StationInfo prevStation = null;
		StationInfo prevPrevStation = null;
		StationInfo nextStation = null;
		PassStation passStation = null;

		while(flag) {
			
			if(lastPath.getPassStationList().size() == 0) {
				Log.d("kts_null", "lastPath.getPassStationList().size() : " + current.getStationCode());
			}
			
			prevStation = this.smallWayPass.getStationInfo(lastPath.getPassStationList().get(lastPath.getPassStationList().size() -1).getStationCode());
			

			// ?�료?�거 체크
			if(!SmallWayChecker.checkLastPath(lastPath)) {
				
				flag = false;
				continue;

			}

		
			if(this.nextFlag == false && lastPath.getStationSet().contains(current.getStationCode())) {
				
				if(!SmallWayChecker.stationLineChecker.isLineMulti(current.getStationCode())
						&& !SmallWayChecker.multiLineChecker.isMultiLine(current.getStationCode())) {
					
					flag = false;
					continue;
				}
				
				
			}
			
			// 굽�??�리?�서 ?�전??�� 길동???�닌 강동???�서 만든???�발.
			
			// �?�� ?�전 ??�� ??��???�이?�랑 ?��???
			if(current.getUpStationCode() != prevStation.getStationCode()
					&&current.getUpStationCode() != 44444 
					&&current.getUpStationCode() != 29000
					&&current.getUpStationCode() != 90000) {
				
				if(!lastPath.getStationSet().contains(current.getUpStationCode()) && !lastPath.isFirstStartFlag()) {
					StationInfo info = this.smallWayPass.getStationInfo(current.getUpStationCode());
					// null ?�수 ?�어 ;; 강동 같�? ?�끼?��? 29000 ?�나 90000 ?�나 44444 같�? ??��?�이 ?�으?�깐
					if(info != null&& info.getUpStationCode() != 29000 && info.getUpStationCode() != 44444 && info.getUpStationCode() != 90000) {
						double time = 0;
						
						StationInfo prevInfo = this.smallWayPass.getStationInfo(info.getUpStationCode());
						// ??��??null ?�수 ?�으?�깐 ??�� ;;
						// ?�승 ;;
						
						// ?��????�면 ;; ??��??갈래길이??개넘?�키
						if( prevInfo == null ){
							time = SmallWayChecker.timeChecker.multiLineTimeCheck(info.getUpStationCode(), info.getUpStationCode(), info.getStationCode());
						}
						else {
							time = prevInfo.getDownStationTime();
						}
						
						lastPath.getPassStationList().add(this.smallWayPass.settingPassStation(info, time));
						lastPath.setLastTime(lastPath.getLastTime() + time);
						lastPath.getStationSet().add(info.getStationCode());
						
					}
				}
				else {
					lastPath.setFirstStartFlag(false);
				}
				
			}
			
			
			
			
			


			

			// ?�전??��???�재??�� 같다�? (?�선�?갈래길때문에 back ?�경??
//			if(prevStation.getStationCode() == current.getStationCode()) {
//				this.smallWayPass.threadRemove(this);
//				flag = false;
//				continue;
//			}



			if(lastPath.getPassStationList().size() > 1) {
				prevPrevStation = this.smallWayPass.getStationInfo(lastPath.getPassStationList().get(lastPath.getPassStationList().size() -2).getStationCode());
			}


			// ?�역 �?��?�서 갈래길이�?
			if(SmallWayChecker.multiLineChecker.isMultiLine(prevStation.getStationCode())){

				double time = 0;
				if(prevPrevStation == null) {
					time = SmallWayChecker.timeChecker.multiLineTimeCheck(prevStation.getStationCode(), prevStation.getStationCode(), current.getStationCode());
				}
				else {
					time = SmallWayChecker.timeChecker.multiLineTimeCheck(prevStation.getStationCode(), prevPrevStation.getStationCode(), current.getStationCode());
				}				

				PassLine passLine = this.smallWayPass.settingPassLine(prevStation, prevStation.getLineNum(), prevStation.getStationCode(), time, 4); // 갈래�?
				lastPath.getPassLineList().add(passLine);
				lastPath.setLastTime(lastPath.getLastTime() + time);

			}

			//빠꾸 방향?�면 죽여버리??

			//?�재�?마�?막이??
//			if(current.getStationCode() == lastPath.getEndStation().getStationCode() || this.smallWayPass.isEndStation(current.getStationCode(), this.lastPath)) {
//				
//			}

			//?�재??�� 목적�?��.!
			if(current.getStationCode() == lastPath.getEndStation().getStationCode() || this.smallWayPass.isEndStation(current.getStationCode(), this.lastPath)) {
				//?�음 ?�거???�보�??�고
				nextStation = this.smallWayPass.getStationInfo(current.getDownStationCode());
				//??��?�한??
				passStation = this.smallWayPass.settingPassStation(current, prevStation.getDownStationTime());
				// 최종 ?�간값에 ?�한??
				lastPath.setLastTime(lastPath.getLastTime() + prevStation.getDownStationTime());

				lastPath.getPassStationList().add(passStation);			
				//그리�??�어�?
				lastPath.getStationSet().add(passStation.getStationCode());
				
				SmallWayChecker.arrivedTargetStation(lastPath);
				
				flag = false;
				continue;
			}

			// ?�음?�거 ?�이 목적�?��!
			if(current.getDownStationCode() == lastPath.getEndStation().getStationCode() || this.smallWayPass.isEndStation(current.getDownStationCode(), this.lastPath)) {
				// ??��?�고 마무�?로직
				Log.d("kts", "THE END");
				//?�음 ?�거???�보�??�고
				nextStation = this.smallWayPass.getStationInfo(current.getDownStationCode());
				//??��?�한??
				passStation = this.smallWayPass.settingPassStation(current, prevStation.getDownStationTime());
				// 최종 ?�간값에 ?�한??
				lastPath.setLastTime(lastPath.getLastTime() + prevStation.getDownStationTime());

				lastPath.getPassStationList().add(passStation);			
				//그리�??�어�?
				lastPath.getStationSet().add(passStation.getStationCode());

				// 최종 ?�거????��
				passStation = this.smallWayPass.settingPassStation(nextStation, current.getDownStationTime());
				lastPath.setLastTime(lastPath.getLastTime() + current.getDownStationTime());
				lastPath.getPassStationList().add(passStation);
				lastPath.getStationSet().add(passStation.getStationCode());
				lastPath.setTheEndFlag(true);

				SmallWayChecker.arrivedTargetStation(lastPath);

				
				flag = false;
				continue;
			}


			// 먼�? 기종 ?�료?�거 ?�는�?�?��
			// ?�료?�게 ?�으�??�재 ?�보??�?��!

			// ?�승??�?��
			if(SmallWayChecker.stationLineChecker.isLineMulti(current.getStationCode())) {

				this.smallWayPass.transferProcess(prevStation, current, lastPath);

				
//				flag = false;
//				continue;
			}

			// 갈래길�???
			if(SmallWayChecker.multiLineChecker.isMultiLine(current.getStationCode())) {
				this.smallWayPass.multiLineProcess(prevStation, current, lastPath);
				
				flag = false;
				continue;
			}






			//?�음 ?�거?�이 마�?막이??
			if(current.getDownStationCode() == 90000 || current.getDownStationCode() == 44444) {

				// ?�발 ?�음?�거?�이 마�?막인?? ??�� 개간???�승??��?? (?�설??
				// 갈래길�???
				if(SmallWayChecker.multiLineChecker.isMultiLine(current.getDownStationCode())) {

					this.smallWayPass.multiLineProcess(prevStation, current, lastPath);

				}

				
				flag = false;
				continue;
			}


			// ??��?�는 로직

			//?�음 ?�거???�보�??�고
			nextStation = this.smallWayPass.getStationInfo(current.getDownStationCode());
			//??��?�한??
			///double time = 0;
			//if(prevPrevStation)
			passStation = this.smallWayPass.settingPassStation(current, prevStation.getDownStationTime());
			// 최종 ?�간값에 ?�한??
			lastPath.setLastTime(lastPath.getLastTime() + prevStation.getDownStationTime());

			lastPath.getPassStationList().add(passStation);			
			//그리�??�어�?
			lastPath.getStationSet().add(passStation.getStationCode());

			Log.d("kts", "down currentn : next = " + current.getStationCode() + " : " + nextStation.getStationCode());

			//?�음껄로 고고~
			current = nextStation;
			this.nextFlag = true;
		}

		this.smallWayPass.threadRemove(this);
	}


}
